package common

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

// 响应模板
type ResponseData struct {
	Code  ResCode     `json:"code"`
	Msg   interface{} `json:"msg"`
	Data  interface{} `json:"data,omitempty"`
	Token interface{} `json:"token,omitempty"`
}

func ResponseError(c *gin.Context, code ResCode) {
	c.JSON(http.StatusOK, &ResponseData{
		Code: code,
		Msg:  code.Msg(),
		Data: nil,
	})
}

func ResponseErrorWithMsg(c *gin.Context, code ResCode, msg interface{}) {
	c.JSON(http.StatusOK, &ResponseData{
		Code: code,
		Msg:  msg,
		Data: nil,
	})
}

func ResponseSuccess(c *gin.Context, data interface{}) {
	c.JSON(http.StatusOK, &ResponseData{
		Code: CodeSuccess,
		Msg:  CodeSuccess.Msg(),
		Data: data,
	})
}

func ResponseTokenSuccess(c *gin.Context, token interface{}) {
	c.JSON(http.StatusOK, &ResponseData{
		Code:  CodeSuccess,
		Msg:   CodeSuccess.Msg(),
		Token: token,
	})
}
